import {ORDER_DEPART, ORDER_DURATION} from './constant'
import {h0} from '../common/fp'

export const ACTION_SET_FROM = 'SET_FROM';
export const ACTION_SET_TO = 'SET_TO';
export const ACTION_SET_DEPART_DATE = 'SET_DEPART_DATE';
export const ACTION_SET_HIGH_SPEED = 'SET_HIGH_SPEED';
export const ACTION_SET_TRAIN_LIST = 'SET_TRAIN_LIST';
export const ACTION_SET_ORDER_TYPE = 'SET_ORDER_TYPE';
export const ACTION_SET_ONLY_TICKETS = 'SET_ONLY_TICKETS';
export const ACTION_SET_TICKET_TYPES = 'SET_TICKET_TYPES';
export const ACTION_SET_CHECKED_TICKET_TYPES = 'SET_CHECKED_TICKET_TYPES';
export const ACTION_SET_TRAIN_TYPES = 'SET_TRAIN_TYPES';
export const ACTION_SET_CHECK_TRAIN_TYPES = 'SET_CHECK_TRAIN_TYPES';
export const ACTION_SET_DEPART_STATIONS = 'SET_DEPART_STATIONS';
export const ACTION_SET_CHECK_DEPART_STATIONS = 'SET_CHECK_DEPART_STATIONS';
export const ACTION_SET_ARRIVE_STATIONS = 'SET_ARRIVE_STATIONS';
export const ACTION_SET_CHECK_ARRIVE_STATIONS = 'SET_CHECK_ARRIVE_STATIONS';
export const ACTION_SET_DEPART_TIME_START = 'SET_DEPART_TIME_START';
export const ACTION_SET_DEPART_TIME_END = 'SET_DEPART_TIME_END';
export const ACTION_SET_ARRIVE_TIME_START = 'SET_ARRIVE_TIME_START';
export const ACTION_SET_ARRIVE_TIME_END = 'SET_ARRIVE_TIME_END';
export const ACTION_SET_IS_FILTERS_OPEN = 'SET_IS_FILTERS_OPEN';
export const ACTION_SET_SEARCH_PARSED = 'SET_SEARCH_PARSED';

export function setFrom (from) {
  return {
    type: ACTION_SET_FROM,
    payLoad: from
  }
}
export function setTo (to) {
  return {
    type: ACTION_SET_TO,
    payLoad: to
  }
}
export function setDepartDate (departDate) {
  return {
    type: ACTION_SET_DEPART_DATE,
    payLoad: departDate
  }
}
export function setHighSpeed (highSpeed) {
  return {
    type: ACTION_SET_HIGH_SPEED,
    payLoad: highSpeed
  }
}
export function toggleHighSpeed (highSpeed) {
  return (dispatch, getState) => {
    const {highSpeed} = getState()
    dispatch(setHighSpeed(!highSpeed))
  }
}
export function setTrainList (trainList) {
  return {
    type: ACTION_SET_TRAIN_LIST,
    payLoad: trainList
  }
}
export function toggleOrderType () {
  return (dispatch, getState) => {
    const {orderType} = getState()
    if(orderType === ORDER_DEPART) {
      dispatch({
        type: ACTION_SET_ORDER_TYPE,
        payLoad: ORDER_DURATION
      })
    }else{
      dispatch({
        type: ACTION_SET_ORDER_TYPE,
        payLoad: ORDER_DEPART
      })
    }
  }
}
export function toggleOnlyTickets () {
  return (dispatch, getState) => {
    const {onlyTickets} = getState()
    dispatch({
      type: ACTION_SET_ONLY_TICKETS,
      payLoad: !onlyTickets
    })
  }
}
export function setTicketTypes (ticketTypes) {
  return {
    type: ACTION_SET_TICKET_TYPES,
    payLoad: ticketTypes
  }
}
export function setCheckedTicketTypes (checkedTicketTypes) {
  return {
    type: ACTION_SET_CHECKED_TICKET_TYPES,
    payLoad: checkedTicketTypes
  }
}
export function setTrainTypes (trainTypes) {
  return {
    type: ACTION_SET_TRAIN_TYPES,
    payLoad: trainTypes
  }
}
export function setCheckTrainTypes (checkTrainTypes) {
  return {
    type: ACTION_SET_CHECK_TRAIN_TYPES,
    payLoad: checkTrainTypes
  }
}
export function setDepartStations (departStations) {
  return {
    type: ACTION_SET_DEPART_STATIONS,
    payLoad: departStations
  }
}
export function setCheckDepartStations (checkDepartStations) {
  return {
    type: ACTION_SET_CHECK_DEPART_STATIONS,
    payLoad: checkDepartStations
  }
}
export function setArriveStations (arriveStations) {
  return {
    type: ACTION_SET_ARRIVE_STATIONS,
    payLoad: arriveStations
  }
}
export function setCheckArriveStations (checkArriveStations) {
  return {
    type: ACTION_SET_CHECK_ARRIVE_STATIONS,
    payLoad: checkArriveStations
  }
}
export function setDepartTimeStart (departTimeStart) {
  return {
    type: ACTION_SET_DEPART_TIME_START,
    payLoad: departTimeStart
  }
}
export function setDepartTimeEnd (departTimeEnd) {
  return {
    type: ACTION_SET_DEPART_TIME_END,
    payLoad: departTimeEnd
  }
}
export function setArriveTimeStart (arriveTimeStart) {
  return {
    type: ACTION_SET_ARRIVE_TIME_START,
    payLoad: arriveTimeStart
  }
}
export function setArriveTimeEnd (arriveTimeEnd) {
  return {
    type: ACTION_SET_ARRIVE_TIME_END,
    payLoad: arriveTimeEnd
  }
}
export function toggleIsFiltersOpen () {
  return (dispatch, getState) => {
    const {isFiltersOpen} = getState()
    dispatch({
      type: ACTION_SET_IS_FILTERS_OPEN,
      payLoad: !isFiltersOpen
    })
  }
}
export function setSearchParsed (searchParsed) {
  return {
    type: ACTION_SET_SEARCH_PARSED,
    payLoad: searchParsed
  }
}
export function toggleSearchParsed () {
  return (dispatch, getState) => {
    const {searchParsed} = getState()
    dispatch({
      type: ACTION_SET_SEARCH_PARSED,
      payLoad: !searchParsed
    })
  }
}

// export function nextDate () {
//   console.log('xx');
//   return (dispatch, getState) => {
//     console.log('123');
//     const {departDate} = getState()
//     dispatch(setDepartDate(h0(departDate) + 86400 * 1000))
//   }
// }
export function nextDate() {
  return (dispatch, getState) => {
      const { departDate } = getState();

      dispatch(setDepartDate(h0(departDate) + 86400 * 1000));
  };
}

export function prevDate () {
  return (dispatch, getState) => {
    const {departDate} = getState()
    dispatch(setDepartDate(h0(departDate) - 86400 * 1000))
  }
}